package com.asas.spaceflight.controller;

import com.asas.spaceflight.controller.PlayerController.Control;
import com.asas.spaceflight.model.Asteroid;
import com.asas.spaceflight.model.Player;
import com.asas.spaceflight.model.World;
import com.asas.spaceflight.view.WorldRenderer;
import com.badlogic.gdx.Input.Keys;

public class GameEngine {
	
	public Player player;
	public World world;
	public WorldRenderer worldRenderer;
	public PlayerController playerController;
	public PhysicalController physicalController;
	public CollisionController collisionController;

	public GameEngine() {
		player = new Player();
		world = new World(player);
		worldRenderer = new WorldRenderer(world);
		physicalController = new PhysicalController(world);
		collisionController = new CollisionController(this);
		playerController = new PlayerController(world, physicalController);
	}
	
	public void start() {
		playerController.start();

		// Dummy stuff
		for (int i = 0; i < 10; i++) {
			Asteroid obj = ObjectFactory.createDummyAsteroid();
			world.getAsteroids().add(obj);
			physicalController.getEntities().add(obj);
		}
		for (int i = 0; i < 2; i++) {
			Asteroid obj = ObjectFactory.createDummyBrideCake();
			world.getAsteroids().add(obj);
			physicalController.getEntities().add(obj);
		}	
	}
	
	public void stop() {
		// TODO
	}
	
	public void update(float delta) {
		physicalController.update(delta);
		collisionController.update();
		playerController.update(delta);
	}
	
	public void render() {
		worldRenderer.render();
	}
	
	public void keyDown(int key) {
		switch (key) {
		case Keys.UP: playerController.react(Control.THROTTLE_PRESSED); break;	
		case Keys.LEFT: playerController.react(Control.LEFT_PRESSED); break;	
		case Keys.RIGHT: playerController.react(Control.RIGHT_PRESSED); break;	
		case Keys.SPACE: playerController.react(Control.FIRE_PRESSED); break;	
		}
	}

	public void keyUp(int key) {
		switch (key) {
		case Keys.UP: playerController.react(Control.THROTTLE_RELEASED); break;	
		case Keys.LEFT: playerController.react(Control.LEFT_RELEASED); break;
		case Keys.RIGHT: playerController.react(Control.RIGHT_RELEASED); break;	
		case Keys.SPACE: playerController.react(Control.FIRE_RELEASED); break;		
		}
	}
	
}
